package com.zoroki.hmusic.controller;


import com.zoroki.hmusic.constant.MessageConstant;
import com.zoroki.hmusic.entity.Comments;
import com.zoroki.hmusic.entity.Result;
import com.zoroki.hmusic.service.ICommentsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zoroki
 * @since 2023-06-11
 */
@RestController
@RequestMapping("/comments")
@CrossOrigin("*")
@Tag(name = "评论")
public class CommentsController {
    @Autowired
    private ICommentsService commentsService;

    @Operation(summary = "根据songId查找评论")
    @GetMapping("/findCommentListFromCommentsBySongId")
    @ResponseBody
    public Result findCommentListFromCommentsBySongId(@RequestParam int songId){
        List<Comments> comments_comment = commentsService.findCommentListFromCommentsBySongId(songId);
        if (comments_comment.size() > 0) {
            return new Result(true, MessageConstant.YES,comments_comment);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据replyId查找回复评论")
    @GetMapping("/findReplyListFromCommentsByReplyId")
    @ResponseBody
    public Result findReplyListFromCommentsByReplyId(@RequestParam int replyId){
        List<Comments> comments_reply = commentsService.findReplyListFromCommentsByReplyId(replyId);
        if (comments_reply.size() > 0) {
            return new Result(true, MessageConstant.YES,comments_reply);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "传入comments来新增评论")
    @PostMapping("/addComments")
    @ResponseBody
    public Result addComments(@RequestBody Comments comments){
        System.out.println(comments.toString());
        Comments newComments = commentsService.addComments(comments);
        if (newComments != null) {
            return new Result(true, MessageConstant.YES,newComments);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据id删除评论")
    @DeleteMapping("/deleteCommentsById")
    @ResponseBody
    public Result deleteCommentById(@RequestParam int id){
        int i = commentsService.deleteCommentById(id);
        if (i > 0) {
            return new Result(true, MessageConstant.YES,null);
        }
        return new Result(false, MessageConstant.NO, null);
    }
}
